# -*- coding: utf-8 -*-
from operations import *
from xml.etree import ElementTree
from services.weixin_service import WeixinService


class WeixinOperation():
	# Exception Aop inject 
	__metaclass__ = OperationAop

	def __init__(self):
		self._weixinService = WeixinService()


	def saveData(self,xmlData):
		xmlRoot = ElementTree.fromstring(xmlData)
		msgType = xmlRoot.find("MsgType").text
		diction = self.__xml2dic(xmlRoot)
		self.__doSwitch(msgType,diction)
		return True

	def __doSwitch(self,key,diction):
		message = {
			MsgType.Text:lambda:self.__doText(diction),
			MsgType.Image:lambda:self.__doImage(diction),
			MsgType.Voice:lambda:self.__doVoice(diction),
			MsgType.Video:lambda:self.__doVideo(diction),
			MsgType.Location:lambda:self.__doMsgLocation(diction),
			MsgType.Link:lambda:self.__doLink(diction)
		}
		message[key]()

	def __doText(self,diction):
		self._weixinService.addText(diction)
		

	def __doImage(self,diction):
		self._weixinService.addImage(diction)

	def __doVoice(self,diction):
		self._weixinService.addVoice(diction)

	def __doVideo(self,diction):
		self._weixinService.addVideo(diction)

	def __doLocation(self,diction):
		self._weixinService.addLocation(diction)

	def __doMsgLocation(self,diction):
		self._weixinService.addMsgLocation(diction)

	def __doLink(self,diction):
		self._weixinService.addLink(diction)

	def __xml2dic(self,xmlRoot):
		diction = {}
		nodes = xmlRoot.getchildren()
		for node in nodes:
			diction[node.tag] = node.text
		return diction



class MsgType():
	
	#文本消息
	Text = "text"
	#图片消息
	Image = "image"
	#语音消息
	Voice = "voice"
	#视频消息
	Video = "video"
	#地理位置消息
	Location = "location"
	#链接消息
	Link = "link"



class EventType():
	
	#订阅
	#1、EventKey无参数 【关注事件】
	#2、EventKey:Qrscene_二维码参数 【用户未关注时，进行关注后的事件推送】
	Subscribe = "subscribe"
	#取消订阅
	Unsubscribe = "unsubscribe"
	#用户已关注时的事件推送
	#EventKey 是一个32位无符号整数
	Scan = "scan"
	#上报地理位置事件
	Location = "LOCATION"
	#自定义菜单事件
	#EventKey：与自定义菜单接口中KEY值对应
	Click = "CLICK"
